import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import '../../../generated/l10n.dart';

class StockQuantDialog extends StatefulWidget {
  StockQuantDialog();

  @override
  _StockQuantDialogState createState() => _StockQuantDialogState();
}

class _StockQuantDialogState extends State<StockQuantDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  StockQuant stockQuant = StockQuant();

  List<StockQuant> selectedList = [];
  //仓库位置id
  List<SelectOptionVO> locationIdList = [];
  //工厂名称
  List<SelectOptionVO> factoryIdList = [];
  //车间名称
  List<SelectOptionVO> workshopIdList = [];

  String? factoryId;
  String? workshopId;

  @override
  void initState() {
    super.initState();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    //仓库位置id
    factoryIdList = await DynamicDataApi.getModelSelect(
        model: "hr.department",
        name: "complete_name",
        key: 'parent_id',
        val: null);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '工厂名称',
            dataList: factoryIdList,
            value: factoryId,
            onSaved: (v) => {factoryId = v},
            onChange: (v) async {
              if (factoryId != v) {
                formKey.currentState!.save();
                factoryId = v;
                workshopId = null;
                stockQuant.locationId = null;
                locationIdList = [];
                try {
                  workshopIdList = await DynamicDataApi.getModelSelect(
                      active: false,
                      model: "hr.department",
                      name: "complete_name",
                      key: 'parent_id',
                      val: int.parse(v));
                } catch (e) {
                  workshopIdList = [];
                  //   stateList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '车间名称',
            dataList: workshopIdList,
            value: workshopId,
            onSaved: (v) => {workshopId = v},
            onChange: (v) async {
              if (workshopId != v) {
                formKey.currentState!.save();
                workshopId = v;
                stockQuant.locationId = null;
                try {
                  locationIdList = await DynamicDataApi.getModelSelect(
                      active: false,
                      model: "stock.location",
                      name: "complete_name",
                      key: 'workshop_id',
                      val: int.parse(v));
                } catch (e) {
                  locationIdList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '库房位置',
            dataList: locationIdList,
            value: stockQuant.locationId,
            onSaved: (v) => {stockQuant.locationId = v},
          ),
          CryInput(
            label: '产品名称',
            value: stockQuant.productName,
            onSaved: (v) {
              stockQuant.productName = v;
            },
          ),
          CryInput(
            label: '生产单号',
            value: stockQuant.productorderno,
            onSaved: (v) {
              stockQuant.productorderno = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, stockQuant);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
